﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DBUtlity;
using Models;
using System.Data;
using System.Data.SqlClient;

namespace DAL
{
    public class ProjectMainService
    {
        #region 新增
        /// <summary>
        /// 添加数据到项目主表
        /// </summary>
        /// <param name="PJMains"></param>
        /// <param name="tableName"></param>
        /// <returns></returns>
        public int InsertProjectInfo(ProjectMain PJMains)
        {
            string sql = "insert into PJMains(WorkParentId,PJId,PJName,Mold,Maktx,MaktxName,BusinessPlate,";
            sql += "PM,StartDate,PJType,MoldType,T1EndDate,T2EndDate,T3EndDate,T4EndDate,T5EndDate)";
            sql += " values(@WorkParentId,@PJId,@PJName,@Mold,@Maktx,@MaktxName,@Customer,@BusinessPlate,";
            sql += "@PM,@StartDate,@PJType,@MoldType,@T1EndDate,@T2EndDate,@T3EndDate,@T4EndDate,@T5EndDate)";
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@WorkParentId",PJMains.WorkParentId),
                new SqlParameter("@PJId",PJMains.PJId),
                new SqlParameter("@PJName",PJMains.PJName),
                new SqlParameter("@Mold",PJMains.Mold),
                new SqlParameter("@Maktx",PJMains.Maktx),
                new SqlParameter("@MaktxName",PJMains.MaktxName),
                new SqlParameter("@Customer",PJMains.Customer),
                new SqlParameter("@BusinessPlate",PJMains.BusinessPlate),
                new SqlParameter("@PM",PJMains.PM),
                new SqlParameter("@StartDate",PJMains.StartDate),
                new SqlParameter("@PJType",PJMains.PJType),
                new SqlParameter("@MoldType",PJMains.MoldType),
                new SqlParameter("@T1EndDate",PJMains.T1EndDate),
                new SqlParameter("@T2EndDate",PJMains.T2EndDate),
                new SqlParameter("@T3EndDate",PJMains.T3EndDate),
                new SqlParameter("@T4EndDate",PJMains.T4EndDate),
                new SqlParameter("@T5EndDate",PJMains.T5EndDate)
            };
            return SQLHelper.Update(sql, param);
        }
        #endregion

        #region 更改
        /// <summary>
        /// 根据工作流号更改项目主表数据
        /// </summary>
        /// <param name="PJMains"></param>
        /// <returns></returns>
        public int UpdateProjectInfo(ProjectMain PJMains)
        {
            string sql = "update PJMains set PJName=@PJname,Mold=@Mold,Maktx=@Maktx,";
            sql += "MaktxName=@MaktxName,BusinessPlate=@BusinessPlate,PM=@PM,";
            sql += "StartDate=@StartDate,PJType=@PJType,MoldType=@MoldType,";
            sql += "T1EndDate=@T1EndDate,T2EndDate=@T2EndDate,T3EndDate=@T3EndDate,";
            sql += "T4EndDate=@T4EndDate,T5EndDate=@T5EndDate,Customer=@Customer";
            sql += " where WorkParentId=@WorkParentId";
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@WorkParentId",PJMains.WorkParentId),
                new SqlParameter("@PJName",PJMains.PJName),
                new SqlParameter("@Mold",PJMains.Mold),
                new SqlParameter("@Maktx",PJMains.Maktx),
                new SqlParameter("@MaktxName",PJMains.MaktxName),
                new SqlParameter("@BusinessPlate",PJMains.BusinessPlate),
                new SqlParameter("@PM",PJMains.PM),
                new SqlParameter("@StartDate",PJMains.StartDate),
                new SqlParameter("@PJType",PJMains.PJType),
                new SqlParameter("@MoldType",PJMains.MoldType),
                new SqlParameter("@T1EndDate",PJMains.T1EndDate),
                new SqlParameter("@T2EndDate",PJMains.T2EndDate),
                new SqlParameter("@T3EndDate",PJMains.T3EndDate),
                new SqlParameter("@T4EndDate",PJMains.T4EndDate),
                new SqlParameter("@T5EndDate",PJMains.T5EndDate),
                new SqlParameter("@Customer",PJMains.T5EndDate)
            };
            return SQLHelper.Update(sql, param);
        }
        /// <summary>
        /// 根据工作流号更改项目主表状态
        /// </summary>
        /// <param name="pjId"></param>
        /// <param name="afterStatues"></param>
        /// <returns></returns>
        public int UpdateProjectInfo(int workParentId,string afterStatues)
        {
            string sql = "update PJMains set Statues=@Statues";
            sql += " where WorkParentId=@WorkParentId";
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@WorkParentId",workParentId),
                new SqlParameter("@Statues",afterStatues)
            };
            return SQLHelper.Update(sql, param);
        }
        #endregion

        #region 查询
        /// <summary>
        /// 根据工作流号查询主表信息
        /// </summary>
        /// <param name="pjId"></param>
        /// <returns></returns>
        public ProjectMain GetProjectInfo(int workParentId)
        {
            string sql = "select * from PJMains where WorkParentId=@WorkParentId";
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@WorkParentId",workParentId)
            };
            SqlDataReader objReader = SQLHelper.GetReader(sql, param);
            ProjectMain ProjectMain = null;
            while(objReader.Read())
            {
                ProjectMain = new ProjectMain()
                {
                    WorkParentId = Convert.ToInt32(objReader["workParentId"]),
                    PJId = objReader["PJId"].ToString(),
                    PJName = objReader["PJName"].ToString(),
                    Mold = objReader["Mold"].ToString(),
                    Maktx = objReader["Maktx"].ToString(),
                    MaktxName = objReader["MaktxName"].ToString(),
                    Customer=objReader["Customer"].ToString(),
                    BusinessPlate = objReader["BusinessPlate"].ToString(),
                    PM = objReader["PM"].ToString(),
                    StartDate = objReader["StartDate"] is DBNull? Convert.ToDateTime("1900-01-01") : Convert.ToDateTime(objReader["StartDate"]),
                    PJType = objReader["PJType"].ToString(),
                    MoldType = objReader["MoldType"].ToString(),
                    T1EndDate = objReader["T1EndDate"] is DBNull ? Convert.ToDateTime("1900-01-01") : Convert.ToDateTime(objReader["T1EndDate"]),
                    T2EndDate = objReader["T2EndDate"] is DBNull ? Convert.ToDateTime("1900-01-01") : Convert.ToDateTime(objReader["T2EndDate"]),
                    T3EndDate = objReader["T3EndDate"] is DBNull ? Convert.ToDateTime("1900-01-01") : Convert.ToDateTime(objReader["T3EndDate"]),
                    T4EndDate = objReader["T4EndDate"] is DBNull ? Convert.ToDateTime("1900-01-01") : Convert.ToDateTime(objReader["T4EndDate"]),
                    T5EndDate = objReader["T5EndDate"] is DBNull ? Convert.ToDateTime("1900-01-01") : Convert.ToDateTime(objReader["T5EndDate"]),
                    Statues = objReader["Statues"].ToString()
                };    
            }
            return ProjectMain;
        }
        /// <summary>
        /// 查询项目主表中是否有相应状态的工作流号
        /// </summary>
        /// <param name="pjId"></param>
        /// <param name="statues"></param>
        /// <returns></returns>
        public int GetProjectInfo(string workParentId, string statues)
        {
            string sql = "select count(*) from PJMains where WorkParentId=@WorkParentId and Statues=@Statues";
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@WorkParentId",workParentId),
                new SqlParameter("@Statues",statues)
            };
            return Convert.ToInt32(SQLHelper.GetSingleResult(sql, param));
        }

        /// <summary>
        /// 获取最后一行项目号
        /// </summary>
        /// <returns></returns>
        public string GetProjectLastRow()
        {
            string sql = "select PJId from PJMains where Id=(select Max(Id) from PJMains)";
            return Convert.ToString(SQLHelper.GetSingleResult(sql));
        }
        #endregion
    }
}
